package com.ityueqiangu.portal.conroller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ityueqiangu.core.constant.Constants;
import com.ityueqiangu.core.enums.CommonEnum;
import com.ityueqiangu.core.enums.SysFlagStatusEnum;
import com.ityueqiangu.core.exception.BizException;
import com.ityueqiangu.core.util.StringUtils;
import com.ityueqiangu.core.web.result.ResultDataUtil;
import com.ityueqiangu.core.web.result.ResultInfo;
import com.ityueqiangu.core.web.vo.ActiverUser;
import com.ityueqiangu.system.domain.SysUser;
import com.ityueqiangu.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Clever、xia
 * @title: FrontUserController
 * @projectName art-exhibition-system
 * @description:
 * @date 2021-03-17 15:31
 */
@Controller
@RequestMapping(value = "/user")
public class FrontUserController {

    @Autowired
    private ISysUserService sysUserService;

    @RequestMapping(value = "/login")
    @ResponseBody
    public ResultInfo login(SysUser sysUser, HttpServletRequest request){
        String sessionCode = (String) request.getSession().getAttribute("randomImage");
        if (!StrUtil.equalsIgnoreCase(sessionCode, sysUser.getRandomImage())) {
            throw new BizException(CommonEnum.RANDOMIMAGE_FAILURE);
        }
        //只查询普通用户的信息
        sysUser.setRoleId(2);
        SysUser currentUser = sysUserService.selectSysUserByLoginName(sysUser);
        if (ObjectUtil.isNull(currentUser)) {
            throw new BizException(CommonEnum.ACCOUNT_NOT_EXIST);
        }
        if(!StrUtil.equals(currentUser.getPassword(),sysUser.getPassword())){
            throw new BizException(CommonEnum.PASSWORD_ERROR);
        }
        if (StrUtil.equals(sysUser.getIsDeleted(), SysFlagStatusEnum.ONE.getKey())) {
            throw new BizException(CommonEnum.ACCOUNT_DISABLED);
        }
        ActiverUser activerUser = new ActiverUser();
        BeanUtils.copyProperties(currentUser,activerUser );
        request.getSession().setAttribute("loginUser",activerUser);
        return ResultDataUtil.createSuccess(CommonEnum.LOGIN_SUCCESS);
    }


    @RequestMapping(value = "/register")
    @ResponseBody
    public ResultInfo register(SysUser sysUser, HttpServletRequest request){
        String sessionCode = (String) request.getSession().getAttribute("randomImage");
        if (!StrUtil.equalsIgnoreCase(sessionCode, sysUser.getRandomImage())) {
            throw new BizException(CommonEnum.RANDOMIMAGE_FAILURE);
        }
        String loginName = sysUserService.register(sysUser);
        return ResultDataUtil.createSuccess(CommonEnum.LOGIN_SUCCESS).setData(StringUtils.format("您注册的用户名称为：{}",loginName));
    }



    @RequestMapping(value = "/personalCenter")
    public String personalCenter(){
        return "/portal/personalCenter";
    }

    @RequestMapping(value = "/leaveMessage")
    public String leaveMessage(){
        return "/portal/leaveMessage";
    }

    @RequestMapping(value = "/logout")
    public String logout(HttpServletRequest request){
        request.getSession().removeAttribute("loginUser");
        return "/front/index";
    }

    @RequestMapping(value = "/checkIsExist")
    @ResponseBody
    public ResultInfo checkIsExist(HttpServletRequest request){
        ActiverUser currentUser = (ActiverUser) request.getSession().getAttribute("loginUser");
        if (ObjectUtil.isNull(currentUser)) {
            throw new BizException(CommonEnum.DATA_NOT_EXIST);
        }
        return ResultDataUtil.createSuccess(CommonEnum.DATA_EXIST);
    }

}
